package com.jiang.demo.depotstable.type;

import com.jiang.demo.depotstable.dds.datasource.DSContextHolder;
import com.jiang.demo.depotstable.domain.Store;
import com.jiang.demo.depotstable.domain.Type;
import com.jiang.demo.depotstable.mapper.StoreMapper;
import com.jiang.demo.depotstable.mapper.TypeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author jiangzhe
 * @create 2021/5/11 16:00
 */
@Slf4j
@Service
public class TypeService {

    @Autowired
    TypeMapper typeMapper;

    @Autowired
    StoreMapper storeMapper;

    @Autowired
    JdbcTemplate jdbcTemplate;

    public static final String tableName_product = "product_";

    public static final String tableName_descript = "descript_";

    /**
     * 新建 type 时:
     * 1.会动态为每个 product_db 数据源新建商品表和描述表即：product_n 和 descript_n
     */
    public void insertStore(Type type){
        DSContextHolder.setDBType("store-db");
        typeMapper.insertType(type);
        List<Store> storeList = storeMapper.getStoreInfos();
        for (Store store: storeList) {
            DSContextHolder.setDBType("product-db-"+store.getName());
            createTable(tableName_product, type.getId());
            log.info("product-db-"+store.getName() + "新建表："+ tableName_product+type.getId());
            createTable(tableName_descript, type.getId());
            log.info("product-db-"+store.getName() + "新建表："+ tableName_descript+type.getId());
        }
    }

    @Transactional
    public void createTable(String tableName, int typeId){
        String sql = "";
        switch (tableName){
            case tableName_product:
                 sql= "CREATE TABLE IF NOT EXISTS product_"+ typeId +" (\n" +
                         "  id int NOT NULL AUTO_INCREMENT,\n" +
                         "  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,\n" +
                         "  price decimal(10,2) DEFAULT NULL,\n" +
                         "  type_id int DEFAULT NULL,\n" +
                         "  store_id int DEFAULT NULL,\n" +
                         "  PRIMARY KEY (id) USING BTREE\n" +
                         ") ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8mb3 ROW_FORMAT=DYNAMIC;";
                 break;
            case tableName_descript:
                sql = "CREATE TABLE IF NOT EXISTS descript_"+ typeId +" (\n" +
                        "  id int NOT NULL AUTO_INCREMENT,\n" +
                        "  `desc` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '商品描述',\n" +
                        "  product_id int DEFAULT NULL,\n" +
                        "  PRIMARY KEY (id) USING BTREE\n" +
                        ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3 ROW_FORMAT=DYNAMIC;";
                break;
        }
        jdbcTemplate.execute(sql);
    }

    public List<Type> getTypeInfos(){
        return typeMapper.getTypeInfos();
    }
}
